package com.study.manage.mvc.org.recommend;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.manage.dal.EvaluateApplyRepository;
import com.study.manage.dal.EvaluateBrandOrgRecommendRepository;
import com.study.manage.dal.EvaluatePriceRepository;
import com.study.manage.dal.OrderRepository;
import com.study.manage.dal.UserRepository;
import com.study.manage.domain.EvaluateApply;
import com.study.manage.domain.EvaluateBrandOrgRecommend;
import com.study.manage.domain.EvaluatePrice;
import com.study.manage.domain.Order;
import com.study.manage.domain.User;
import com.study.manage.domain.enumm.ItemType;
import com.study.manage.domain.enumm.LiveDieState;
import com.study.manage.domain.enumm.UserType;
import com.study.manage.dto.AjaxResponse;
import com.study.manage.dto.JsonResponseBean;
import com.study.manage.mvc.common.BaseController;
import com.study.manage.utils.MapUtils;

@Controller
@RequestMapping("org/evaluate/recommend")
public class RecommendController extends BaseController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private EvaluateApplyRepository evaluateApplyRepository;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private EvaluatePriceRepository evaluatePriceRepository;
	
	@Autowired
	private EvaluateBrandOrgRecommendRepository evaluateBrandOrgRecommendRepository;
	
    @RequestMapping(path = "init", method = RequestMethod.GET)
    public String init(Long id,ModelMap model) {

		model.put("evaluateId", id);
        return "org/evaluate/cmm000001";
    }

    @ResponseBody
    @RequestMapping(path = "getRecommends", method = RequestMethod.GET)
    public JsonResponseBean getPageDatas(Long evaluateId,
    		@PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {
    	
        Page<EvaluateBrandOrgRecommend> page = evaluateBrandOrgRecommendRepository.findRecommends(evaluateId, LiveDieState.LIVE, pageable);
        Object obj = page.map(new Converter<EvaluateBrandOrgRecommend, Map<String, Object>>() {
            @Override
            public Map<String, Object> convert(EvaluateBrandOrgRecommend source) {
            	Map<String, Object> map = MapUtils.convert(source);
            	User user = userRepository.findOne(source.userId);
            	map.put("tjName", user.name);
            	map.put("type", UserType.getName(user.type.getIndex()));
            	if(source.brandOrgId!=null){
                	map.put("yjName", userRepository.findOne(source.userId).name);
            	}
            	map.put("userNumber", evaluateApplyRepository.getEvaluateIdApplyCount(Arrays.asList(evaluateId),null,user.id));
                return map;
            }
        });
        return new JsonResponseBean(obj,true);
    }
    
    @RequestMapping(path = "user", method = RequestMethod.GET)
    public String user(Long userId,Long evaluateId,ModelMap model) {

    	model.put("evaluateId", evaluateId);
    	model.put("userId", userId);
        return "org/evaluate/cmm000002";
    }
    
    @ResponseBody
    @RequestMapping(path = "getUserTableDatas", method = RequestMethod.GET)
    public AjaxResponse info_list(Long evaluateId,Long userId,
            @PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {
    	
        AjaxResponse ajax = new AjaxResponse(true);
        Page<EvaluateApply> page = evaluateApplyRepository.findEvaluateApplys(evaluateId,null,null, null, null, null,userId, pageable); 
        return ajax.setObj(page.map(new Converter<EvaluateApply, Map<String, Object>>() {
            @Override
            public Map<String, Object> convert(EvaluateApply source) {
                Map<String, Object> map = MapUtils.convert(source);
                User user = userRepository.findOne(source.userId);
                map.put("userNumber", user.number); 
                map.put("userName", user.name);
                if(source.verify!=null) map.put("verifyDesc", source.verify.desc);
                map.put("stateDesc", source.state.desc);
                if (source.referenceId != null) {
                    User refUer = userRepository.findOne(source.referenceId);
                    map.put("refName", refUer.name);
                }
                if (source.evaluatePriceId != null) {
                    EvaluatePrice ep = evaluatePriceRepository.findOne(source.evaluatePriceId);
                    map.put("epName", ep.title + "(" + ep.charge + ")");
                }
                Order order = orderRepository.findByItemTypeAndItemIdAndUserId(ItemType.EvaluateSignup, source.evaluateId, source.userId);
                if (order != null) {
                    map.put("orderItemPrice", order.itemPrice);
                }
                return map;
            }
        }));
    }
    
/*    @ResponseBody
    @Transactional
    @RequestMapping(path = "delete", method = RequestMethod.POST)
    public JsonResponseBean updateRecommend(Long id) {

    	return new JsonResponseBean(true);
    }*/
    
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
